package com.moter.demo01.juc.blockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 同步队列,一次只能有一个放在队列里面,
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"取"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"取"+blockingQueue.take());
                System.out.println(Thread.currentThread().getName()+"取"+blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                blockingQueue.put("a");
                System.out.println(Thread.currentThread().getName()+"放"+"a");
                TimeUnit.SECONDS.sleep(3);
                blockingQueue.put("a");
                System.out.println(Thread.currentThread().getName()+"放"+"a");
                TimeUnit.SECONDS.sleep(3);
                blockingQueue.put("a");
                System.out.println(Thread.currentThread().getName()+"放"+"a");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }
}
